package manager.pay;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Timer;
import java.util.TimerTask;


import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


import org.springframework.beans.BeansException;

import com.fasterxml.jackson.core.JsonProcessingException;


import database.DBService;

import database.pojo.UserConsumeInfo;
import database.pojo.UserRechargeInfo;
import javassist.expr.NewArray;
import manager.connection.ClientConnectionManager;
import manager.connection.type.GMOrderInfo;
import manager.pay.state.ConsumptionType;
import manager.pay.state.PayState;
import manager.pay.type.EndTimeInfo;
import manager.pay.type.PayResultInfo;
import manager.pay.type.ntfPlayerPayStatusInfo;
import manager.pay.type.ntfWeChatPayInfo;
import manager.player.PlayerManager;
import manager.qrcode.QRCodeManager;
import net.socket.server.SocketServer;
import net.socket.server.protocal.PROTOID;
import net.socket.server.request.IRequest;
import net.socket.server.request.PlayerLogoutRequest;
import net.socket.server.response.NTFVipGradeResponse;
import util.MyFactory;

/**
 * 类名：GetPayResult
 * 描述：处理支付回调,并通知客户端玩家时间over
 * @date 2018-4-24
 * @author zk
 * @version 0.0.1
 *
 */
public class GetPayResult {
	private ClientConnectionManager ccm = null;
	private PlayerManager pm = null;
	private Map<String, EndTimeInfo>  endTimeMap = new HashMap<String,EndTimeInfo>();
	
	private ArrayList<URI> ntfPHPFailList = new ArrayList<URI>();
	
	public void init(){
		
		
		ccm = MyFactory.context.getBean("ClientConnectionManager", ClientConnectionManager.class);
		Timer timer = new Timer();
		TimerTask taskClient =ntfClient();
		timer.schedule(taskClient,0, 1000);
		
	}
	
	
	

	
	
	private TimerTask  ntfClient() {
		TimerTask task = new TimerTask() {
			
		@Override
		public void run() {
			if(endTimeMap.size() !=0) {
				try {
					ntfClientTimeOver();
				} catch (IllegalArgumentException | IllegalAccessException | JsonProcessingException e) {
					System.out.println("ntfClientGameOver have a Exception:"+e);
				} catch (BeansException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (URISyntaxException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}	
		};
		return task;
		
	}
	
	
	private void ntfClientTimeOver() throws IllegalArgumentException, IllegalAccessException, BeansException, SQLException, URISyntaxException, IOException {
		EndTimeInfo eInfo =null;
		Iterator<Entry<String, EndTimeInfo>> entries = endTimeMap.entrySet().iterator();  
		while (entries.hasNext()) {  
		  Map.Entry<String, EndTimeInfo> entry = entries.next();   
		  eInfo = entry.getValue();
		  
		  if(eInfo.getEndTime() <= System.currentTimeMillis()) {
			  System.out.println("NTF_TIME_IS_OVER________________________________");
			  System.out.println(eInfo.getMid());
			synchronized(endTimeMap) {
				  entries.remove(); 
			 }
			
			//时间到重新生成房间二维码通知客户端
			String QrcodeUrl= MyFactory.context.getBean("QRCodeManager",QRCodeManager.class).getQRCode(eInfo.getMid());
			eInfo.setQrcodeUrl(QrcodeUrl);
			ccm.sendResponse(PROTOID.NTF_TIME_IS_OVER, eInfo.getMid(), eInfo);
			MyFactory.context.getBean("DBService", DBService.class).updateEndtime(eInfo.getMid(), 0);
			System.out.println("ntfClientTimeOver-----------timeover noti .");
		}
	}	
	}
	
	public void putMap(String MachineId,EndTimeInfo eTimeInfo) {
		synchronized(endTimeMap) {
			endTimeMap.put(MachineId, eTimeInfo);
		}
	}
	
	public void removeMap(String MachineId) {
		synchronized (endTimeMap) {
			endTimeMap.remove(MachineId);
		}
	}
	
	
	public EndTimeInfo getMap(String MachineId) {
			return endTimeMap.get(MachineId);
		
	}
	
	public Boolean containsMap(String MachineId) {
		return endTimeMap.containsKey(MachineId);
	}


	

}
